package com.ml.framework.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类，常用方法
 * 
 * 
 */
public class DateTimeUtils
{
	// 年-月-日 时:分:秒 毫秒
	public static final String FMT_FULL_TIME = "yyyy-MM-dd HH:mm:ss SSS";

	// 年-月-日 时:分:秒
	public static final String FMT_YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";

	// 年月日时分秒
	public static final String FMT_YYYYMMDDHHMMSS_SEQ = "yyyyMMddHHmmss";

	// 年月日时分秒毫秒
	public static final String FMT_YYYYMMDDHHMMSS_S_SEQ = "yyyyMMddHHmmssSSS";

	// 年-月-日 时:分
	public static final String FMT_YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";

	// 年-月-日
	public static final String FMT_YYYYMMDD = "yyyy-MM-dd";
	
	// 年-月-日
	public static final String FMT_YYYYMMDD_SEQ = "yyyyMMdd";

	// 年-月
	public static final String FMT_YYYYMM = "yyyy-MM";
	// 年-月
	public static final String FMT_YYYYMM_SEQ = "yyyyMM";

	// 时:分:秒
	public static final String FMT_HHMMSS = "HH:mm:ss";

	// 时:分
	public static final String FMT_HHMM = "HH:mm";

	private static final String[] DATETIME_FORMAT_STRING = { FMT_FULL_TIME, FMT_YYYYMMDDHHMMSS, FMT_YYYYMMDDHHMMSS_SEQ, FMT_YYYYMMDDHHMM, FMT_YYYYMMDD, FMT_YYYYMM, FMT_HHMMSS, FMT_HHMM };

	public static final int MORE = 1;

	public static final int LESS = -1;

	public static final int EQUAL = 0;

	/**
	 * 转换成Date对象
	 * 
	 * @param str
	 * @param fmt
	 * @return
	 */
	public static Date convertToDate(String str, String fmt)
	{
		if (str == null)
			return null;

		try
		{
			return new SimpleDateFormat(fmt).parse(str);
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 转换成Date对象
	 * 
	 * @param str
	 * @return
	 */
	public static Date convertToDate(String str)
	{
		if (str == null)
			return null;
		str = str.trim();
		if (str.length() == 0)
			return null;

		// 去掉json-lib加的T字母
		str = str.replace('T', ' ');

		for (String format : DATETIME_FORMAT_STRING)
		{
			if (str.length() == format.length())
			{
				Date d = convertToDate(str, format);
				if (d != null)
					return d;
			}
		}
		return null;
	}

	/**
	 * 
	 * 按指定的格式，转换成Date对象
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String convertToString(java.util.Date date, String format)
	{
		if (date == null)
			return null;

		String sdate = null;
		try
		{
			sdate = new SimpleDateFormat(format).format(date);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return sdate;
	}

	/**
	 * 比较两个日期的大小
	 * 
	 * @param date1
	 * @param date2
	 * @return 小返回-1，大返回1，相等返回0
	 */
	public static int compareDate(Date date1, Date date2)
	{
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		// different date might have different offset
		cal1.setTime(date1);
		long ldate1 = date1.getTime() + cal1.get(Calendar.ZONE_OFFSET) + cal1.get(Calendar.DST_OFFSET);

		cal2.setTime(date2);
		long ldate2 = date2.getTime() + cal2.get(Calendar.ZONE_OFFSET) + cal2.get(Calendar.DST_OFFSET);

		long dateDiff = ldate1 - ldate2;
		return dateDiff > 0 ? MORE : (dateDiff == 0 ? EQUAL : LESS);
	}

	/**
	 * 比较两个日期相隔的时间秒
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long getDiffSeconds(Date date1, Date date2)
	{
		long milsec1 = date1.getTime();
		long milsec2 = date2.getTime();
		return (milsec2 - milsec1) / 1000;
	}

	/**
	 * 比较两个日期相隔的天数
	 * 
	 * @param date1
	 * @param date2
	 * @return date2-date1
	 */
	public static int getDiffDays(Date date1, Date date2)
	{
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		// different date might have different offset
		cal1.setTime(date1);
		long ldate1 = date1.getTime() + cal1.get(Calendar.ZONE_OFFSET) + cal1.get(Calendar.DST_OFFSET);

		cal2.setTime(date2);
		long ldate2 = date2.getTime() + cal2.get(Calendar.ZONE_OFFSET) + cal2.get(Calendar.DST_OFFSET);

		// Use integer calculation, truncate the decimals
		int hr1 = (int) (ldate1 / 3600000); // 60*60*1000
		int hr2 = (int) (ldate2 / 3600000);

		int days1 = (int) hr1 / 24;
		int days2 = (int) hr2 / 24;

		int dateDiff = days2 - days1;
		return dateDiff;
	}

	/**
	 * 得到当前日期的年
	 * 
	 * @param
	 * @return year
	 */
	public static int getCurrentYear()
	{
		return Calendar.getInstance().get(Calendar.YEAR);
	}

	/**
	 * 得到当前日期的月
	 * 
	 * @param
	 * @return month
	 */
	public static int getCurrentMonth()
	{
		return Calendar.getInstance().get(Calendar.MONTH) + 1;
	}

	/**
	 * 得到当前日期的日
	 * 
	 * @param
	 * @return month
	 */
	public static int getCurrentDay()
	{
		return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 得到指定日期年份的第一天
	 */
	public static Date getYearFirstDay(Date date)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 将日期设置为当月第一天
		calendar.set(calendar.get(Calendar.YEAR), 0, 1, 0, 0, 0);
		return calendar.getTime();
	}

	/**
	 * 得到指定时间的所属月份的第一天
	 */
	public static Date getMonthFirstDay(Date date)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 将日期设置为当月第一天
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1, 0, 0, 0);
		return calendar.getTime();
	}

	/**
	 * 得到指定时间的星期1日期
	 */
	public static Date getWeekFirstDay(Date date)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_WEEK, (-1) * calendar.get(Calendar.DAY_OF_WEEK) + 2);
		return calendar.getTime();
	}

	/**
	 * 得到指定时间加上增量后的时间
	 * 
	 * @param date
	 * @param add
	 * @return
	 */
	public static Date getAddDate(Date date, int type, int v)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if (type == 1)
			calendar.add(Calendar.YEAR, v);
		else if (type == 2)
			calendar.add(Calendar.MONTH, v);
		else if (type == 3)
			calendar.add(Calendar.DAY_OF_YEAR, v);
		else if (type == 4)
			calendar.add(Calendar.HOUR_OF_DAY, v);
		else if (type == 5)
			calendar.add(Calendar.MINUTE, v);
		else if (type == 6)
			calendar.add(Calendar.SECOND, v);
		return calendar.getTime();
	}

	/**
	 * 得到指定日期的最大天数(年、月)
	 * 
	 * @param date
	 * @return
	 */
	public static int getMaxDays(Date date, int type)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int md = 0;
		if (type == 1)
			md = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
		else if (type == 2)
			md = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		return md;
	}
}
